package sudoku.server.persistencia.sqlserver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Administrador
 */
public class Agente {
	private static Agente yo;
	protected Connection mBD;
	protected static final int SQLSERVER = 0, ACCESS = 1; 
	protected String[] mURLs = {
			"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=sudokus",
			"jdbc:odbc:sudokusaccess"
	};
	protected String[] mDrivers = {
			"com.microsoft.jdbc.sqlserver.SQLServerDriver",
			"sun.jdbc.odbc.JdbcOdbcDriver"
	};
	protected String[] mUsers = {
			"sa",
			""
	};
    protected Agente() throws SQLException {
		try {
			Class.forName(mDrivers[ACCESS]);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(0);
        }
		mBD=DriverManager.getConnection(mURLs[ACCESS], mUsers[ACCESS], "");
	}
	
	public static Agente getAgente() throws SQLException {
		if (yo==null) 
			yo=new Agente();
		if (yo.mBD.isClosed())
		    yo.mBD=DriverManager.getConnection(yo.mURLs[ACCESS], yo.mUsers[ACCESS], "");
		return yo;
	}
	
	public Connection getDB() throws SQLException {
		if (mBD.isClosed())
			mBD=DriverManager.getConnection(mURLs[ACCESS], mUsers[ACCESS],"");
		return mBD;
	}
}
